import 'dart:async';

import 'package:flutter/material.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../base/MyScaffold.dart';
import '../../config/Constant.dart';
import '../../model/Bus.dart';
import '../../res.dart';
import '../../utils/EventBus/EventBus.dart';
import '../../utils/Tool/MyTools.dart';
import '../../utils/color/ColorUtils.dart';
import '../../utils/userbloc/user_bloc.dart';
import '../convert/AllFile.dart';
import '../convert/StarMark.dart';
import '../dialog/SortDialog.dart';
import '../process/Processing.dart';
import '../search/Search.dart';
import '../splash/SplashPage.dart';
import '../subscribe/Subscribe.dart';

class FileHome extends StatefulWidget {
  const FileHome({super.key});

  @override
  State<FileHome> createState() => _HomePageState();
}

class _HomePageState extends State<FileHome>
    with SingleTickerProviderStateMixin {
  TextEditingController controller = TextEditingController();
  String name = "";
  List<Tab> tabs = [
    Tab(
      text: 'all_files'.tr,
    ),
    Tab(
      text: 'star_mark'.tr,
    ),
  ];
  List<Widget> tabViews = [
    Allfile(
      star: false,
    ),
    const StarMark()
  ];
  late TabController tabController;

  StreamSubscription? subscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabController = TabController(length: tabs.length, vsync: this);
    subscription = EventBusUtil.addListener<Bus>((event) {
      print("USER_INFORMATION_UPDATE----");
      if (event.label == Constant.USER_INFORMATION_UPDATE) {
        var lang = SpUtil.getString(Constant.lanuage);
        var code = SpUtil.getString(Constant.code);
        if (!MyTools.stringIsEmpty(lang)) {
          var locale = Locale(lang!, code);
          Get.updateLocale(locale);
        }
        initState();
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    EventBusUtil.removeListener(subscription!);
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      navBarHide: true,
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        margin: const EdgeInsets.only(top: 16),
        child: Column(
          children: [
            Row(
              children: [
                MyLocalImage(
                  Res.sys_img_logo3x,
                  height: 30,
                  onclick: () async {
                    // Directory directory = await getTemporaryDirectory();
                    // CacheUtil.delDir(directory);
                    //Get.to(const MyHomePage());
                    //MyToast().show("MyHomePageMyHomePageMyHomePageMyHomePage");
                    String? first = SpUtil.getString(Constant.first);
                    MyToast().show(first.toString());
                  },
                ),
                const Spacer(),
                if (UserBlocTool.getUser()?.is_vip == "0" ||
                    !UserBlocTool.isLogin())
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: InkWell(
                      onTap: ()  {
                        Get.to(const Subscribe());
                        SpUtil.putString(Constant.first, "1111111");
                      },
                      child: const Image(
                        image: AssetImage(
                          Res.nav_icon_getpro3x,
                        ),
                        width: 52,
                        height: 24,
                      ),
                    ),
                  ),
                InkWell(
                  onTap: () {
                    Get.to(const Processing());
                  },
                  child: const Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    child: MyLocalImage(
                      Res.sys_icon_task,
                      width: 24,
                      height: 24,
                    ),
                  ),
                ),
                MyLocalImage(
                  Res.sys_icon_sidebar,
                  width: 24,
                  height: 24,
                  margin: EdgeInsets.all(4),
                  onclick: () {
                    Scaffold.of(context).openDrawer();
                    EventBusUtil.fireEvent(
                        Bus(Constant.USER_INFORMATION_UPDATE, null));
                  },
                )
              ],
            ),
            InkWell(
              onTap: () {
                Get.to(const Search());
              },
              child: Container(
                margin: const EdgeInsets.symmetric(vertical: 10),
                padding: const EdgeInsets.symmetric(horizontal: 10),
                height: 40,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(6),
                    color: rgba(247, 247, 247, 1)),
                child: Row(
                  children: [
                    const MyLocalImage(
                      Res.nav_icon_search,
                      width: 20,
                      height: 20,
                    ),
                    TextView(
                      "search_files".tr,
                      margin: const EdgeInsets.symmetric(horizontal: 10),
                      style: TextStyle(color: rgba(200, 202, 204, 1)),
                      onclick: () {},
                    )
                  ],
                ),
              ),
            ),
            Row(children: [
              Expanded(
                flex: 10,
                child: TabBar(
                  tabs: tabs,
                  unselectedLabelColor: ColorUtils.text,
                  labelStyle:
                      TextStyle(color: rgba(255, 78, 77, 1), fontSize: 14),
                  controller: tabController,
                  isScrollable: false,
                  indicatorColor: Colors.red,
                  labelColor: Colors.red,
                ),
              ),
              const Spacer(
                flex: 6,
              ),
              MyLocalImage(
                Res.sys_icon_seq_otn,
                width: 20,
                height: 20,
                margin: const EdgeInsets.all(10),
                onclick: () {
                  Get.bottomSheet(const SortDialog());
                },
              )
            ]),
            const Divider(
              height: 1,
            ),
            Expanded(
              child: TabBarView(
                controller: tabController,
                children: tabViews,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
